package com.lysj.bhp.investment.controller;

import com.lysj.bhp.investment.base.InvestmentContent;
import com.lysj.bhp.investment.domain.Dividend;
import com.lysj.bhp.util.random.FormatUtil;
import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.util.web.Resp;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.Bidi;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/dividend")
public class DividendController extends InvestmentContent {

    @InitBinder
    public void InitBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, null, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }

    @RequestMapping("/list")
    public Resp list() {
        List<Dividend> dividends = dividendRepository.findAll();
        return new Resp(dividends);
    }


    /**
     * @author drj
     * @date 2018/06/13 19:35
     * @Description:显示昨日利息
     */
    @GetMapping("/last_dividend")
    public BigDecimal lastDividend(Date date, Long userId) {
        Date startDate = FormatUtil.getDayStartDay(date);
        Date endDate = FormatUtil.getDayEndDay(date);
        Dividend dividend = dividendRepository.findByUserIdAndCreateTimeBetween(userId, startDate, endDate);

        if (null == dividend) {
            return new BigDecimal(BigInteger.ZERO);
        }

        return dividend.getDividend();

    }

}
